package com.showde.shoppingmall.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.ProductCollectionConstant;
import com.showde.shoppingmall.db.constant.ProductConstant;
import com.showde.shoppingmall.domain.ProductCollection;

public class ProductCollectionDBCommand implements
		DBCommandInteface<ProductCollection, String> {

	private static final String TAG = "ProductCollectionDBCommand";

	DBSession dbSession;

	public ProductCollectionDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(ProductCollection t) {
		ContentValues contentValues = new ContentValues();
		contentValues
				.put(ProductCollectionConstant.PRODUCTID, t.getProductId());
		contentValues.put(ProductCollectionConstant.PRODCUTTIME,
				new Date().getTime());
		dbSession.insert(ProductCollectionConstant.TABLE_NAME, contentValues,
				SQLiteDatabase.CONFLICT_ABORT);
	}

	@Override
	public void add(List<ProductCollection> ts) {
		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (ProductCollection collection : ts) {
			ContentValues contentValues = new ContentValues();
			contentValues.put(ProductCollectionConstant.PRODUCTID,
					collection.getProductId());
			contentValues.put(ProductCollectionConstant.PRODCUTTIME,
					collection.getCollectiontime());
			contentValuesList.add(contentValues);
		}
		dbSession.insertBatch(ProductCollectionConstant.TABLE_NAME,
				contentValuesList, SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void delete(String pk) {
		dbSession
				.delete(ProductCollectionConstant.TABLE_NAME,
						ProductCollectionConstant.PRODUCTID + "=?",
						new String[] { pk });
	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public ProductCollection query(String pk) {
		return null;
	}

	@Override
	public List<ProductCollection> query(SQLiteCriteria sqLiteCriteria) {
		return null;
	}

	@Override
	public List<ProductCollection> queryAll() {
		final List<ProductCollection> productCollections = new ArrayList<ProductCollection>();
		String sql = "select * from " + ProductCollectionConstant.TABLE_NAME
				+ " pc left join " + ProductConstant.TABLENAME + " p on pc."
				+ ProductCollectionConstant.PRODUCTID + "=p."
				+ ProductConstant.PRODUCTID;
		dbSession.query(sql, null, new QueryCompleteCallback() {

			@Override
			public void onQueryError(String errorMsg) {
				Log.e(TAG, errorMsg);
			}

			@Override
			public void onQueryComplete(Cursor cursor) {
				if (cursor == null || cursor.getCount() == 0) {
					return;
				}

				ProductCollection productCollection = null;
				while (cursor.moveToNext()) {
					productCollection = new ProductCollection();
					productCollection.setProductId(dbSession.getString(cursor,
							ProductCollectionConstant.PRODUCTID));
					productCollection.setProductTitle(dbSession.getString(
							cursor, ProductConstant.PRODUCTTITLE));
					productCollection.setProductLogo(dbSession.getString(
							cursor, ProductConstant.PRODUCTLOGO));
					productCollection.setCollectiontime(dbSession.getString(
							cursor, ProductCollectionConstant.PRODCUTTIME));
					productCollections.add(productCollection);
				}
			}
		});
		return productCollections;
	}

	public List<ProductCollection> queryAllSimple() {
		final List<ProductCollection> productCollections = new ArrayList<ProductCollection>();
		String sql = "select * from " + ProductCollectionConstant.TABLE_NAME
				+ " order by " + ProductCollectionConstant.PRODCUTTIME
				+ " desc";
		dbSession.query(sql, null, new QueryCompleteCallback() {

			@Override
			public void onQueryError(String errorMsg) {
				Log.e(TAG, errorMsg);
			}

			@Override
			public void onQueryComplete(Cursor cursor) {
				if (cursor == null || cursor.getCount() == 0) {
					return;
				}

				ProductCollection productCollection = null;
				while (cursor.moveToNext()) {
					productCollection = new ProductCollection();
					productCollection.setProductId(dbSession.getString(cursor,
							ProductCollectionConstant.PRODUCTID));
					productCollection.setCollectiontime(dbSession.getString(
							cursor, ProductCollectionConstant.PRODCUTTIME));
					productCollections.add(productCollection);
				}
			}
		});
		return productCollections;
	}

	public boolean exist(String productId) {
		String sql = "select count(*) pcount from "
				+ ProductCollectionConstant.TABLE_NAME + " where "
				+ ProductCollectionConstant.PRODUCTID + "=?";
		Cursor cursor = null;
		try {
			cursor = dbSession.rawQuery(sql, new String[] { productId });
			if (cursor != null && cursor.getCount() > 0 && cursor.moveToNext()) {
				return dbSession.getInt(cursor, "pcount") > 0;
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return false;
	}

}
